package com.eblacorp.archive.service.impl;

import java.beans.IntrospectionException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import com.eblacorp.archive.annotations.FnTransactional;
import com.eblacorp.archive.dao.fn.RealtyDocumentDao;
import com.eblacorp.archive.domain.fn.RealtyDocument;
import com.eblacorp.archive.exceptions.NoAttachedFnDocument;
import com.eblacorp.archive.exceptions.NotFnDocumentException;
import com.eblacorp.archive.exceptions.NullValueException;
import com.eblacorp.archive.service.RealtyDocumentService;
import com.filenet.api.constants.CheckinType;

public class RealtyDocumentServiceImpl implements RealtyDocumentService {

	private RealtyDocumentDao realtyDocumentDao;

	@Override
	@FnTransactional
	public List<RealtyDocument> search(RealtyDocument realtyDocument,
			Integer startPage, Integer pageSize, boolean withDocument)
			throws IllegalArgumentException, NotFnDocumentException,
			IntrospectionException, IllegalAccessException,
			InvocationTargetException, InstantiationException {
		realtyDocument.setCurrentVersion(true);
		return realtyDocumentDao.getByExample(realtyDocument, null, null,
				withDocument);
	}

	@Override
	@FnTransactional
	public RealtyDocument addNewDocument(RealtyDocument realtyDocument,
			File file) throws IllegalArgumentException, NotFnDocumentException,
			IntrospectionException, IllegalAccessException,
			InvocationTargetException, InstantiationException,
			NullValueException, IOException {
		RealtyDocument newRealtyDocument = null;
		realtyDocument.setDocumentTitle(realtyDocument
				.getRealtyRegistrationNumber());
		if (file == null) {
			newRealtyDocument = realtyDocumentDao.create(realtyDocument, file,
					CheckinType.MINOR_VERSION);
		} else {
			newRealtyDocument = realtyDocumentDao.create(realtyDocument, file,
					CheckinType.MAJOR_VERSION);
		}
		return newRealtyDocument;
	}
	@Override
	@FnTransactional
	public RealtyDocument updateDocument(RealtyDocument realtyDocument,
			File file) throws IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException, NullValueException, IOException {
		realtyDocument.setDocumentTitle(realtyDocument.getRealtyRegistrationNumber());
		if(file!=null){
			realtyDocument = realtyDocumentDao.update(realtyDocument, file, CheckinType.MAJOR_VERSION);
		}else{
			realtyDocument = realtyDocumentDao.update(realtyDocument, null, null);
		}
		return realtyDocument;
	}
	
	@Override
	@FnTransactional
	public void deleteDocument(RealtyDocument realtyDocument, boolean allVersion) throws IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException, NullValueException {
		realtyDocumentDao.delete(realtyDocument, allVersion);
	}
	
	@Override
	public String getViewerUrl(RealtyDocument realtyDocument) {
		return realtyDocumentDao.getWcmJavaViewerUrl(realtyDocument);
	}
	
	@Override
	@FnTransactional
	public byte[] download(RealtyDocument realtyDocument) throws NoAttachedFnDocument, IOException {
		
		return realtyDocumentDao.download(realtyDocument);
	}
	
	public RealtyDocumentDao getRealtyDocumentDao() {
		return realtyDocumentDao;
	}

	public void setRealtyDocumentDao(RealtyDocumentDao realtyDocumentDao) {
		this.realtyDocumentDao = realtyDocumentDao;
	}

}
